Imports DotNetNuke.UI.WebControls
Imports System.IO

Partial Class DNNMenu
	Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

	End Sub
	Protected WithEvents Checkbox1 As System.Web.UI.WebControls.CheckBox

	'NOTE: The following placeholder declaration is required by the Web Form Designer.
	'Do not delete or move it.
	Private designerPlaceholderDeclaration As System.Object

	Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
		'CODEGEN: This method call is required by the Web Form Designer
		'Do not modify it using the code editor.
		InitializeComponent()
	End Sub

#End Region

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Put user code to initialize the page here
		Me.chkUseTables.Attributes.Add("onclick", "alert('Warning! tableless menus will not work well with the new css layout of the site in Internet Explorer.');")
		SetMenuDefaults()
        'Me.MyDNNMenu.MenuNodes(1).Selected = True
	End Sub

	Private Sub SetMenuDefaults()
        Me.chkPopulateFromClient.Enabled = Not Me.chkForceDownLevel.Checked
        If Me.chkPopulateFromClient.Enabled = False Then Me.chkPopulateFromClient.Checked = False
        Me.MyDNNMenu.PopulateNodesFromClient = Me.chkPopulateFromClient.Checked
        Me.MyDNNMenu.UseTables = Me.chkUseTables.Checked
        Me.MyDNNMenu.ForceDownLevel = chkForceDownLevel.Checked
        Me.MyDNNMenu.IsCrawler = Me.chkIsCrawler.Checked
        Me.MyDNNMenu.Orientation = Me.ddlOrientation.SelectedValue
        Me.MyDNNMenu.SubMenuOrientation = Me.ddlSubOrientation.SelectedValue
        'Me.MyDNNMenu.CheckBoxes = chkCheckboxes.Checked
        Me.MyDNNMenu.JSFunction = "myTestFunction('TestParam1', 'TestParam2');"
        Me.MyDNNMenu.Target = "MyTarget"
        Me.MyDNNMenu.MouseInDelay = CInt(Me.txtMouseInDelay.Text)
        Me.MyDNNMenu.MouseOutDelay = CInt(Me.txtMouseOutDelay.Text)
        Me.MyDNNMenu.EnablePostbackState = Me.chkEnablePostbackState.Checked

        Me.txtDir.Text = Request.MapPath("")
        If Not IsPostBack OrElse Me.MyDNNMenu.EnablePostbackState = False Then
            MyDNNMenu.ImageList.Add("images/folder.gif")
            MyDNNMenu.ImageList.Add("images/file.gif")
            'MyDNNMenu.IndentWidth = 20
            'MyDNNMenu.UseClientEvents = True
            PopulateMenu()
        ElseIf Me.MyDNNMenu.MenuNodes.Count > 0 AndAlso Me.txtDir.Text.ToLower <> Me.MyDNNMenu.MenuNodes(0).Key.ToLower Then
            PopulateMenu()
        End If


        If Me.IsPostBack Then
            Me.txtStatus.Value = "Postback occurred: " & Now.ToString & vbCrLf & Me.txtStatus.Value
        End If

        Dim sTable As String = "Table"
        Dim sOrientation As String = "V"
        If MyDNNMenu.UseTables = False Then sTable = "NoTable"
        If MyDNNMenu.Orientation = Orientation.Horizontal Then sOrientation = "H"
        Me.phCss.Controls.Add(New LiteralControl("<LINK href=""DNNMenu" & sOrientation & sTable & ".css"" type=""text/css"" rel=""stylesheet"">"))

    End Sub

	Private Sub PopulateMenu()
		MyDNNMenu.MenuNodes.Clear()
        PopulateChildrenMenuNodes(Nothing)

	End Sub

	Private Sub PopulateChildrenMenuNodes(ByVal objParent As MenuNode)
		Dim strKey As String = Me.txtDir.Text
		If Not objParent Is Nothing Then
			strKey = objParent.Key
		End If
		If strKey.EndsWith("\") = False Then strKey &= "\"
		Dim files As String() = Directory.GetFileSystemEntries(strKey)
		Dim index As Integer = 0
        While index < files.Length AndAlso index < 15         'hack to only allow 5 items per menu
            Dim objFileInfo As FileInfo = New FileInfo(files(index))
            Dim objMenuNode As MenuNode
            Dim intIndex As Integer
            If objParent Is Nothing Then
                intIndex = MyDNNMenu.MenuNodes.Add()
                objMenuNode = MyDNNMenu.MenuNodes(intIndex)
            Else
                intIndex = objParent.MenuNodes.Add()                 'New MenuNode(objParent.XmlNode, MyDNNMenu)
                objMenuNode = objParent.MenuNodes(intIndex)
            End If
            objMenuNode.Text = objFileInfo.Name
            objMenuNode.Key = objFileInfo.FullName
            objMenuNode.ToolTip = objFileInfo.Name
            'objMenuNode.LeftHTML = "lefthtml"
            'objMenuNode.RightHTML = "righthtml"
            'objMenuNode.PopulateOnDemand = True

            If objMenuNode.Key.EndsWith(".js") Then
                objMenuNode.JSFunction = "myJSExtFunction('" & objMenuNode.Key & "');"
                objMenuNode.CSSClass = "SpecialNode"
                objMenuNode.CssClassOver = "SpecialNodeHover"
            End If
            'if file is htm and contained is relative to the currently browsed page then allow navigation
            If objMenuNode.Key.EndsWith(".htm") AndAlso objMenuNode.Key.IndexOf(Server.MapPath(Request.ApplicationPath)) > -1 Then
                Dim sNav As String = objMenuNode.Key.Replace(Server.MapPath(Request.ApplicationPath & "\"), "").Replace("\", "/")
                objMenuNode.JSFunction = "myJSNavFunction('" & sNav & "');"
                objMenuNode.ClickAction = eClickAction.Navigate
                objMenuNode.NavigateURL = sNav
            End If

            If objMenuNode.Key.EndsWith(".vb") Then
                objMenuNode.ClickAction = eClickAction.None
                objMenuNode.CSSClass = "DisabledNode"
                objMenuNode.Enabled = False
            End If

            If (File.GetAttributes(files(index)) And FileAttributes.Directory) = FileAttributes.Directory Then
                objMenuNode.ImageIndex = eImageType.Folder
                'if we are not populating from client we need to populate all the MenuNodes now
                If Me.MyDNNMenu.PopulateNodesFromClient = False Then PopulateChildrenMenuNodes(objMenuNode)
                Try
                    Dim objDir As DirectoryInfo = New DirectoryInfo(objFileInfo.FullName)
                    objMenuNode.HasNodes = objDir.GetFiles.Length > 0 OrElse objDir.GetDirectories.Length > 0
                    objMenuNode.ClickAction = eClickAction.Expand
                Catch ex As Exception
                End Try
            Else
                'objMenuNode.JSFunction = "myTestFunction('a', 1);"
                objMenuNode.ImageIndex = eImageType.Page
            End If
            index += 1
        End While
	End Sub

    Private Sub MyDNNMenu_PopulateOnDemand(ByVal source As Object, ByVal e As DNNMenuEventArgs) Handles MyDNNMenu.PopulateOnDemand
        PopulateChildrenMenuNodes(e.Node)
    End Sub

	Public Enum eImageType
		Folder
		Page
	End Enum

	Private Sub MyDNNMenu_NodeClick(ByVal source As Object, ByVal e As DNNMenuNodeClickEventArgs) Handles MyDNNMenu.NodeClick
        Dim nodeInfo As String = ""
		nodeInfo &= "<b>Node ID:  </b>"
		nodeInfo &= e.Node.ID & "<br>"
		nodeInfo &= "<b>Node Text:  </b>"
		nodeInfo &= e.Node.Text & "<br>"
		nodeInfo &= "<b>Node Tooltip:  </b>"
		nodeInfo &= e.Node.ToolTip & "<br>"
		nodeInfo &= "<b>Node HasChildren:  </b>"
		nodeInfo &= e.Node.HasNodes.ToString
		If e.Node.HasNodes Then
			nodeInfo &= "<br><b>Node Expanded:  </b>"
			'nodeInfo &= e.Node.IsExpanded.ToString
		End If
		nodeInfo &= "<br>"

		lblEventDetails.Text = nodeInfo
	End Sub

	Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
		MyBase.OnInit(e)
		DotNetNuke.UI.Utilities.ClientAPI.HandleClientAPICallbackEvent(Me)
	End Sub

	Private Sub chkPopulateFromClient_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPopulateFromClient.CheckedChanged
		PopulateMenu()
	End Sub

	Private Sub chkForceDownLevel_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkForceDownLevel.CheckedChanged
		PopulateMenu()
	End Sub

	Private Sub chkEnablePostbackState_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEnablePostbackState.CheckedChanged
		PopulateMenu()
	End Sub

End Class
